// 导入路径辅助模块，避免重复引用
const pathImport = require('../helpers/path_import.js');
const { pageHelper, cloudHelper } = pathImport;;
const ProjectBiz = require('../../../comm/biz/project_biz.js');
const FlowBiz = require('../../../biz/flow_biz.js');
const PassportBiz = require('../../../../../config/passport_biz_config.js');

Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		isLoad: false,

		tabCur: 0,
		mainCur: 0,
		verticalNavTop: 0,

		type: 'my', //my我的, all全部

		cateList: FlowBiz.getCateList(),
		datalist: [],

		time: '',

		search: '',

		isAdmin: false, //是否管理员
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: async function (options) {
		ProjectBiz.initPage(this);

		// 强制设置登录状态
		FlowBiz.forceSetLoginState(this, 'flow_list');

		this._loadList(1);

		let type = options.type;
		if (type && type == 'run') {
			this.setData({
				type: 'all'
			});
		}

		this.setData({
			isAdmin: FlowBiz.isAdmin()
		})
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {
		// 每次显示页面时强制设置登录状态
		FlowBiz.forceSetLoginState(this, 'flow_list');
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: async function () {
		// 强制设置登录状态
		FlowBiz.forceSetLoginState(this, 'flow_list');
		
		await this._loadList(1);
		this.selectComponent("#cmpt-comm-list").setPage(1);
		wx.stopPullDownRefresh();
	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},

	url: function (e) {
		// 强制设置登录状态
		FlowBiz.forceSetLoginState(this, 'flow_list');
		
		pageHelper.url(e, this);
	},

	bindCancelTap: async function (e) {
		// 强制设置登录状态
		FlowBiz.forceSetLoginState(this, 'flow_list');
		
		let callback = async () => {
			pageHelper.showLoading('处理中');
			let id = pageHelper.dataset(e, 'id');

			try {
				let params = {
					id
				}
				let opts = {
					title: 'bar'
				}
				await cloudHelper.callCloudData('flow/cancel', params, opts);
				pageHelper.showSuccToast('取消成功', 1500);
				this.setData({
					isLoad: false
				});
				this._loadList(1);
			} catch (err) {
				console.log(err);
			}
		}

		pageHelper.showConfirm('确认取消?', callback);
	},

	_setSearchShow: function (flag = true) {
		this.setData({
			searchShow: flag
		})
	},

	_loadList: async function (page) {
		// 强制设置登录状态
		FlowBiz.forceSetLoginState(this, 'flow_list');
		
		if (!pageHelper.getOptions(this, undefined, false)) return;

		if (!await PassportBiz.loginMustCancelWin(this)) return;

		this.setData({
			isLoad: false
		});

		let params = {
			search: this.data.search,
			sortType: this.data.sortType,
			sortVal: this.data.sortVal,
			cateId: this.data.cateId,
			type: this.data.type,
		};

		let opts = {
			title: 'bar'
		}
		await cloudHelper.dataList(this, 'flow/list', params, opts);

		this.setData({
			time: pageHelper.fmtNow()
		});

	},

	myCommListListener: function (e) {
		// 强制设置登录状态
		FlowBiz.forceSetLoginState(this, 'flow_list');
		
		this.setData({
			isLoad: false
		});

		pageHelper.commListListener(this, e);
	},

	bindVerticalNavTap: function (e) {
		// 强制设置登录状态
		FlowBiz.forceSetLoginState(this, 'flow_list');
		
		let id = e.currentTarget.dataset.id;
		let index = e.currentTarget.dataset.index;

		let cateId = '';
		if (id != '0') cateId = id;

		//初始化判断
		let tabCur = 0;
		for (let k = 0; k < this.data.cateList.length; k++) {
			if (this.data.cateList[k].id == cateId) {
				tabCur = k;
				break;
			}
		}

		//按钮 
		this.setData({
			tabCur,
			mainCur: tabCur,
			verticalNavTop: (tabCur - 1) * 45,
			isLoad: false,
			cateId,
		});


		this._loadList(1);
	},

	bindSearchCmpt: function (e) {
		// 强制设置登录状态
		FlowBiz.forceSetLoginState(this, 'flow_list');
		
		this.setData({
			search: e.detail,
			isLoad: false
		});
		this._loadList(1);
	},

	bindDelTap: async function (e) {
		// 强制设置登录状态
		FlowBiz.forceSetLoginState(this, 'flow_list');
		
		let id = pageHelper.dataset(e, 'id');
		let title = encodeURIComponent(pageHelper.dataset(e, 'title'));
		let type = this.data.type;
		let callback = async () => {
			try {
				let params = {
					id
				}
				await cloudHelper.callCloud('flow/del', params, {});
				pageHelper.delListNode(id, this.data.dataList.list);
				this.data.dataList.total--;
				this.setData({
					dataList: this.data.dataList
				});
				pageHelper.showSuccToast('删除成功');
			} catch (e) {
				console.log(e);
			}
		}
		pageHelper.showConfirm('确认删除? 删除后不可恢复', callback);
	}
}) 